﻿using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace EzDeploy.Core
{
    public class ProjectService : IProjectService
    {
        public void Save(Project project, IEnumerable<DeployGridItem> deploymentItems, string fileName)
        {
            project.Assemblies.Clear();

            foreach (DeployGridItem item in deploymentItems)
                project.Assemblies.Add(item.AssemblyName.FullName, item.DeploymentType);

            string json = JsonConvert.SerializeObject(project);
            File.WriteAllText(fileName, json);
        }

        public Project Load(string fileName)
        {
            if (File.Exists(fileName))
            {
                string json = File.ReadAllText(fileName);
                return JsonConvert.DeserializeObject<Project>(json);
            }

            return new Project();
        }
    }
}
